package com.ruoyi.daily.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.daily.domain.dto.*;
import com.ruoyi.daily.domain.vo.ByDateAndPageVo;
import com.ruoyi.daily.domain.vo.PageVoAndDateOrYearMonth;
import com.ruoyi.daily.mapper.MonitoringStationMapper;
import com.ruoyi.daily.mapper.WorkOrderManagementMapper;
import com.ruoyi.daily.service.WorkOrderManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author cmz
 * @Date 2024-05-31 15:27
 */
@Service
public class WorkOrderManagementServiceImpl implements WorkOrderManagementService {
    @Resource
    private WorkOrderManagementMapper workOrderManagementMapper;
    @Resource
    private MonitoringStationMapper monitoringStationMapper;
    @Override
    public R<LossRateDto> lossRate(PageVoAndDateOrYearMonth byDateAndPageVo) {
        ByDateAndPageVo vo = new ByDateAndPageVo();
        vo.setPageDate(byDateAndPageVo.getPageDate());
        if (byDateAndPageVo.getDate()!=null) {
            vo.setStartTime(byDateAndPageVo.getDate());
            vo.setEndTime(byDateAndPageVo.getDate());
        }else if (byDateAndPageVo.getYearMonth()!=null && !byDateAndPageVo.getYearMonth().equals("")){
            String yearMonthStr = byDateAndPageVo.getYearMonth();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
            YearMonth yearMonth = YearMonth.parse(yearMonthStr,formatter);

            // 获取月份开始的日期  
            LocalDate firstDayOfMonth = yearMonth.atDay(1);

            // 获取月份结束的日期  
            // 使用TemporalAdjusters来获取最后一天的日期  
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
            
            vo.setStartTime(convertLocalDateToDate(firstDayOfMonth));
            vo.setEndTime(convertLocalDateToDate(lastDayOfMonth));
        }
        LossRateDto map = workOrderManagementMapper.lossRate(vo);
        PageHelper.startPage(byDateAndPageVo.getPageDate());
        List<LossListDto> list =  workOrderManagementMapper.lossList(vo);
        map.setList(list);
        return R.ok(map);
    }

    @Override
    public R<ReturnOfGoodsTrendArrayDto> returnOfGoodsTrend() {
        List<ReturnOfGoodsTrendDto> returnOfGoodsTrendDto = workOrderManagementMapper.returnOfGoodsTrend();
        returnOfGoodsTrendDto.sort((a,b)->a.getDateStr().compareTo(b.getDateStr()));
        ReturnOfGoodsTrendArrayDto arrayDto = new ReturnOfGoodsTrendArrayDto();
        
        List<String> dateStrs = new ArrayList<>();

        List<Double> timelinessRate = new ArrayList<>();
        List<Integer> notTimely = new ArrayList<>();
        List<Integer> checkCount = new ArrayList<>();
        
        for (ReturnOfGoodsTrendDto ofGoodsTrendDto : returnOfGoodsTrendDto) {
            dateStrs.add(ofGoodsTrendDto.getDateStr());
            timelinessRate.add(ofGoodsTrendDto.getTimelinessRate());
            notTimely.add(ofGoodsTrendDto.getNotTimely());
            checkCount.add(ofGoodsTrendDto.getCheckCount());
        }

        List<NameAndListValueEchartsDto> list = new ArrayList<>();
        NameAndListValueEchartsDto nameTimelinessRate = new NameAndListValueEchartsDto();
        nameTimelinessRate.setName("及时率");
        nameTimelinessRate.setValues(timelinessRate);
        list.add(nameTimelinessRate);

        NameAndListValueEchartsDto nameNotTimely = new NameAndListValueEchartsDto();
        nameNotTimely.setName("未及时量");
        nameNotTimely.setValues(notTimely);
        list.add(nameNotTimely);

        NameAndListValueEchartsDto nameCheckCount = new NameAndListValueEchartsDto();
        nameCheckCount.setName("考核量");
        nameCheckCount.setValues(checkCount);
        list.add(nameCheckCount);

        arrayDto.setDateStrs(dateStrs);
        arrayDto.setNameAndListValueDtos(list);
        
        return R.ok(arrayDto);
    }

    @Override
    public R todayReturnOfGoods() {
        Map<String,Object> map = workOrderManagementMapper.todayReturnOfGoods();
        return R.ok(map);
    }

    @Override
    public R variousReturnOfGoods() {
        List<Map<String,Object>> map = workOrderManagementMapper.variousReturnOfGoods();
        return R.ok(map);
    }

    @Override
    public R workOrderManagement() {
        WorkOrderManagementDto map = workOrderManagementMapper.workOrderManagement();
        List<WorkOrderManagementEchartsDto> list = workOrderManagementMapper.workOrderManagementList();
        Map<String,Object> map1 = new HashMap<>();
        List<String> datestrList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        for (WorkOrderManagementEchartsDto workOrderManagementEchartsDto : list) {
            datestrList.add(workOrderManagementEchartsDto.getDatestr());
            countList.add(workOrderManagementEchartsDto.getCount());
        }
        map1.put("datestrList",datestrList);
        map1.put("countList",countList);
        //map.setList(list);
        map.setMap(map1);
        return R.ok(map);
    }

    @Override
    public R<Map<String, Object>> returnBarLineEcharts() {
        List<ReturnOfGoodsTrendDto> returnOfGoodsTrendDto = workOrderManagementMapper.returnOfGoodsTrend();
        returnOfGoodsTrendDto.sort((a,b)->a.getDateStr().compareTo(b.getDateStr()));
        Map<String, Object> arrayDto = new HashMap<>();

        List<String> dateStrs = new ArrayList<>();

        List<Double> timelinessRate = new ArrayList<>();
        List<Integer> notTimely = new ArrayList<>();
        List<Integer> checkCount = new ArrayList<>();

        for (ReturnOfGoodsTrendDto ofGoodsTrendDto : returnOfGoodsTrendDto) {
            dateStrs.add(ofGoodsTrendDto.getDateStr());
            timelinessRate.add(ofGoodsTrendDto.getTimelinessRate());
            notTimely.add(ofGoodsTrendDto.getNotTimely());
            checkCount.add(ofGoodsTrendDto.getCheckCount());
        }

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","及时率");
        nameTimelinessRate.put("values",timelinessRate);
        nameTimelinessRate.put("type","line");
        nameTimelinessRate.put("yAxisIndex",1);
        list.add(nameTimelinessRate);

        Map<String,Object> nameNotTimely = new HashMap<>();
        nameNotTimely.put("name","未及时量");
        nameNotTimely.put("values",notTimely);
        nameNotTimely.put("type","bar");
        nameNotTimely.put("stack","bar");
        list.add(nameNotTimely);

        Map<String,Object> nameCheckCount = new HashMap<>();
        nameCheckCount.put("name","考核量");
        nameCheckCount.put("values",checkCount);
        nameCheckCount.put("type","bar");
        nameCheckCount.put("stack","bar");
        list.add(nameCheckCount);

        List<Map<String,Object>> yAxis = new ArrayList<>();
        HashMap<String, Object> yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","单位(元)");
        HashMap<String, Object> axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value}");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);

        yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","");
        axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value} %");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);
        
        arrayDto.put("dateStrs",dateStrs);
        arrayDto.put("nameAndListValueDtos",list);
        arrayDto.put("yAxis",yAxis);
        return R.ok(arrayDto);
    }

    public static Date convertLocalDateToDate(LocalDate localDate) {
        // 将LocalDate转换为ZonedDateTime，需要指定时区  
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        // 将ZonedDateTime转换为Date  
        return Date.from(zonedDateTime.toInstant());
    }
}
